package no.ntnu.fp.gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.*;
import javax.swing.text.PasswordView;

import no.ntnu.fp.model.RunApp;

/**
 * Denne klassen lager et simpelt login-panel med GridBagLayout
 *  
 * @author Kristian
 */

public class GUILogin extends JFrame implements ActionListener {

	
	
	/*public static void main(String args[]) {
		GUILogin gui = new GUILogin();
	}*/

	//Variabler
	JTextField loginField;
	JTextField passwordField;
	JButton loginButton;
	JFrame frame;
	JLabel loginLabel;
	
	//Userinput
	private String username;
	private String password;
	
	private boolean actionPerformed;

	public GUILogin() {
		//username = "";
		//password = "";
		actionPerformed = false;
		GridBagConstraints c = new GridBagConstraints();

		//Selve framen
		frame = new JFrame("Kalender LOGIN");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(300, 100);
		frame.setLayout(new GridBagLayout());
		frame.setResizable(false);
		
		
		//Login textlabel
		loginLabel = new JLabel("Username:");
		c.weightx = 0.1;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(0, 0, 0, 0);
		frame.add(loginLabel, c);
		
		
		//Login textfield
		loginField = new JTextField();
		c.weightx = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c. gridy = 0;
		c.insets = new Insets(0, 0, 0, 0);
		frame.add(loginField, c);

		//Password textfield
		passwordField = new JTextField();
		c.weightx = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 2;
		c. gridy = 0;
		c.insets = new Insets(0, 0, 0, 0);
		frame.add(passwordField, c);
		
		//Login button
		loginButton = new JButton("Logg inn");
		c.weightx = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 3;
		c.gridy = 0;
		c.insets = new Insets(0, 0, 0, 0);
		frame.add(loginButton, c);
		
		loginButton.addActionListener(this);
	}
	
	/**
	 * Denne metoden blir brukt i klassen RunApp. Vet ikke om man egentlig trenger den, eller hvordan den
	 * eventuelt skal implementeres.
	 */
	public void showScreen(){
		frame.setVisible(true);
	}
	
	public void hideScreen(){
		frame.setVisible(false);
	}
	
	/**
	 * Metoden brukes av klassen RunApp for aa se hvilken bruker som proever aa logge seg inn.
	 * @return username typed into the textfield
	 * @throws IOException if no username entered
	 */
	public String requestUsername() throws IOException{
		/*while (!actionPerformed) {
		}*/
		if(username.equals(null)){
			throw new IOException("There is no username.");
		}
		
		return username;
	}

	/**
	 * Metoden brukes av klassen RunApp for aa sjekke om brukeren har skrevet inn rett passord.
	 * @return password typed into the textfield
	 * @throws IOException if no password entered
	 */
	public String requestPassword() throws IOException{
		/*while (!actionPerformed) {
		}*/
		if(password.equals(null)){
			throw new IOException("There is no password.");
		}
		
		return password;
	}

	/**
	 * Metoden registrerer brukerens input i to String-objekter.
	 */
	public void actionPerformed(ActionEvent evt) {
		username = loginField.getText();
		password = loginField.getText();
		actionPerformed = true;
	}
}